#ifndef FADE_OPAQUE_INCLUDE
#define FADE_OPAQUE_INCLUDE

    float FadeClipValue(half transparency, float4 screenPos)
    {
        #if defined(UNITY_PASS_SHADOWCASTER)
            return step(transparency, 0.01);
        #else
            // Screen-door transparency: Discard pixel if below threshold.
            float4x4 thresholdMatrix =
            {
                1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
                13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
                4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
                16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
            };
            // half tempTran = min(transparency, colorA);
            float4x4 rowAccess = 
            { 
                1, 0, 0, 0, 
                0, 1, 0, 0, 
                0, 0, 1, 0, 
                0, 0, 0, 1 
            };
            float2 pos = screenPos.xy / screenPos.w;
            pos *= _ScreenParams.xy;
            return saturate(dot(thresholdMatrix[fmod(pos.x, 4)], rowAccess[fmod(pos.y, 4)]) - transparency);
        #endif
    }

    void FadeOpaque(in real cutOff, in real opaque, in real4 screenPos, out real clipValue)
    {
        #if defined(_FADE_OPAQUE)
            clipValue = FadeClipValue(opaque, screenPos);
            real threshold = (step(cutOff, opaque) - 0.1) * 0.02;
            clipValue = step(clipValue, threshold);
        #else
            clipValue = opaque;
        #endif
    }

    #define FadeOpaque_float(cutOff,alpha,spos,clipV) FadeOpaque(cutOff,alpha,spos,clipV)
    #define FadeOpaque_half(cutOff,alpha,spos,clipV) FadeOpaque(cutOff,alpha,spos,clipV)

#endif